package com.yksj.healthtalk.adapter;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;

public class SalonAdapter extends BaseAdapter{
	List<GroupInfoEntity> salonEntities;
	private LayoutInflater inflater;
	onClickSalonHeadListener listener;
	onClickSalonAttentionListener mAttentionListener ;
	private ImageLoader mImageLoader;
	viewHolder holder;
	private AppData appData;
	
	public SalonAdapter(Context context, List<GroupInfoEntity> salonEntities) {
		super();
		inflater = LayoutInflater.from(context);
		this.salonEntities = salonEntities;
		mImageLoader = ImageLoader.getInstance();
		appData = HTalkApplication.getAppData();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return salonEntities.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return salonEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	//获取翻页的标记
	public String getPageFlagMark(boolean isFristPage){
		if (salonEntities !=  null && salonEntities.size() > 0 && !isFristPage) {
			return salonEntities.get(salonEntities.size()-1).getFlag(); 
		}else {
			return "";
		}
	}
	
	//硬性排序标记
	public String getFlagPlacingMark(boolean isFristPage){
		if (salonEntities !=  null && salonEntities.size() > 0 && !isFristPage) {
			return salonEntities.get(salonEntities.size()-1).getFlagPlacing(); 
		}else {
			return "-100000";
		}
	}
	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final GroupInfoEntity  mSalonName = salonEntities.get(position);
		if (convertView == null || convertView.getTag() == null) {
			holder = new viewHolder();
			convertView = inflater.inflate(R.layout.salon_item,null) ;
			holder.head = (ImageView) convertView
					.findViewById(R.id.head_image);
			holder.messageNum = (TextView) convertView.findViewById(R.id.message_num);
			holder.salonName = (TextView) convertView.findViewById(R.id.salon_name_tv);
			holder.salonLevel = (ImageView) convertView.findViewById(R.id.salon_level_iv);
			holder.doctorSalon = (ImageView) convertView.findViewById(R.id.doctor_salon_iv);
			holder.chargeSalon = (ImageView) convertView.findViewById(R.id.charge_salon_iv);
			holder.attentionSalon = (ImageView) convertView.findViewById(R.id.attention_salon_iv);
			holder.onlineNum = (TextView)convertView.findViewById(R.id.salon_online_num);
			holder.note = (TextView) convertView
					.findViewById(R.id.note);
			convertView.setTag(holder);
		} else {
			holder = (viewHolder) convertView.getTag();
		}
		
		holder.salonName.setText(mSalonName.getName());
		/**
		 * 初始化数据
		 */
		holder.messageNum.setVisibility(View.VISIBLE);
		int size = appData.getNoReadMesgSize(mSalonName.getId());
		if (appData.getNoReadMesgSize(mSalonName.getId()) != 0) {
			holder.messageNum.setText(size + "");
		} else {
			holder.messageNum.setVisibility(View.GONE);
		}
		//判断是否是医师沙龙
		if (!mSalonName.isSalon()) {
			holder.doctorSalon.setVisibility(View.GONE);
		}else {
			holder.doctorSalon.setVisibility(View.VISIBLE);
		}
		
		String string = mSalonName.getGroupLevel();
		if(NumberUtils.isNumber(string)){
			int lev = Integer.valueOf(string);
			if(lev < 6 && lev >0){
				holder.salonLevel.setVisibility(View.VISIBLE);
				holder.salonLevel.getDrawable().setLevel(lev);
			}else {
				holder.salonLevel.setVisibility(View.GONE);
			}
		}
		
		//是否收费
	if (!mSalonName.isCharge()) {
			holder.chargeSalon.setVisibility(View.GONE);
		}else {
			holder.chargeSalon.setVisibility(View.VISIBLE);
		}
	
	//沙龙头像
	mImageLoader.displayImage("4",mSalonName.getNormalHeadIcon(),holder.head);
	
	//关注状态 1已收藏 
		if (mSalonName.isSalonAttention()) {
			holder.attentionSalon.setImageResource(R.drawable.not_attention);
		}else {
			holder.attentionSalon.setImageResource(R.drawable.attention);
		}
	
		holder.attentionSalon.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if (mAttentionListener != null) {
				mAttentionListener.onSalonAttentionClick(holder.attentionSalon, mSalonName, position);
			}
		}
	});
	//签名
	holder.note.setText(mSalonName.getRecordDesc());
	//在线人数
	holder.onlineNum.setText(mSalonName.getPersonNumber().equals("")?"0人":mSalonName.getPersonNumber());
	
	/**
	 * 	点击事件
	 *  */
	holder.head.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if (listener != null) {
				listener.onSalonHeadClick(holder.head,mSalonName,position);
			}
		}
	});
	return convertView;
	}
	
	static  class viewHolder {
		TextView note;
		ImageView head; // 头像
		TextView messageNum;//信息数量
		TextView salonName; // 名称
		ImageView salonLevel; // 等级
		ImageView doctorSalon;//医师沙龙
		ImageView chargeSalon;//付费的沙龙
		ImageView attentionSalon;//是否是关注的沙龙
		TextView onlineNum;//在线人数
	}
	
	public  interface onClickSalonHeadListener{
		void onSalonHeadClick(View v,GroupInfoEntity mSalonName,int position);
	}
	
	public void setonClickSalonHeadListener(onClickSalonHeadListener l){
		this.listener = l;
	}
	
	public interface onClickSalonAttentionListener{
		void onSalonAttentionClick(View v,GroupInfoEntity mSalonName,int positon);
	}
	
	public void setonClickSalonAttentionListener(onClickSalonAttentionListener l){
		this.mAttentionListener = l;
	}

}
